<?php

class Ticket extends AppModel {

/**
 * Name
 *
 * @var string
 */
    public $name = "Ticket";
    
/**
 * belongsTo
 *
 * @var array
 */

    var $belongsTo = array(
        'Customer' => array(
            'className' => 'Customer'
        )
    );
/**
 * hasMany
 *
 * @var array
 */

    var $hasMany = array(
        'TicketMessage' => array(
            'className' => 'TicketMessage'
        ),
        'TicketSla' => array(
            'className' => 'TicketSla'
        ),
        'TicketReason' => array(
            'className' => 'TicketReason'
        )                   
    );          


/**
 * virtual fields
 *
 * @var string
 **/    
    public $virtualFields = array(
        'name' => 'CONCAT(Ticket.first_name, " ", Ticket.last_name)'
    );

/**
 * Validation rules
 *
 * @var string
 **/    
    public $validate = array(
            'first_name' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter your first name'
                )
            ),  
            'last_name' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter your last name'
                )
            ),  
            'mobile' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter your mobile'
                )
            ),  
            'subject' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter subject'
                )
            ),  
            'email' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter Email'
                ),
                'rule-2' => array ( 
                    'rule' => 'email',
                    'message' => 'Please enter a valid email'
                )
            ), 
            'description' => array(
                'rule-1' => array(
                    'rule' => 'notEmpty',  
                    'message' => 'Please enter description.'
                )
            )               
        );


/**
 * @method: checkSla
 * @purpose: to check if sla should be incremented
 * @param $id
 * @return none
 **/
    function checkSla( $id = null, $user = null ) {
         $result = $this->find('first', array('conditions' => array('Ticket.id' => $id), 'field' => 'sla', 'recursive' => -1));
         $first = $result['Ticket']['sla'];
         $subTable = $this->TicketSla->find('first', array('conditions' => array('TicketSla.ticket_id' => $id), 'order' => 'TicketSla.sla DESC'));
         if(!empty($subTable)) {
            $second = $subTable['TicketSla']['sla'];
         } else {
            $second = 0;
         }
         
         if($first > $second) {
               $data['TicketSla']['ticket_id'] = $id;
               $data['TicketSla']['sla'] = $first;
               $current_time = date('Y-m-d H:i:s');
               if(empty($subTable)) {
                    $response_time = abs(strtotime($current_time) - strtotime($result['Ticket']['created']));
               } else {
                    $response_time = abs(strtotime($current_time) - strtotime($subTable['TicketSla']['created']));
               }
               $data['TicketSla']['response_time'] = $response_time;
               $data['TicketSla']['user_id'] = $user;
               $this->TicketSla->save($data);
         }
    }


    function makeAssocOnFly(){
        $this->bindModel(
                array('hasMany' => array(
                        'User' => array(
                            'className' => 'User',
                            'foreignKey' => false,
                            'conditions' => array('User.id' => 'TicketSla.user_id')
                        )
                    )
                )
            );
    }

}